﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebEdition;
using maptest.Common;
using Constants = maptest.Common.Constants;

namespace maptest
{
    public partial class admin : System.Web.UI.Page
    {
        private sql data = new sql();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                
                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
                Map1.MapUnit = GeographyUnit.Meter;
                Map1.ZoomLevelSet = CustomZoomlevelSets();
                //Map1.MapTools.MouseCoordinate.Enabled = true;
                GoogleOverlay mapgg4 = new GoogleOverlay();
                mapgg4.GoogleMapType = GoogleMapType.Normal;
                mapgg4.Name = "google";
                Map1.CustomOverlays.Add(mapgg4);
                DDLayer.DataValueField = "TABLE_NAME";
                DDLayer.DataTextField = "TABLE_NAME";
                DDLayer.DataSource = data.get("SELECT TABLE_NAME FROM information_schema.tables").Tables[0];
                DDLayer.DataBind();
                txtColor.Enabled = false;
                LblType.Text = "Polygon";
                Session["View"] = "";
            }
            
        }

        private void ApplyStyleAreA(string layerSQL)
        {
            
            //he toa do
            Proj4Projection proj4 = new Proj4Projection();
            proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
            proj4.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();
            proj4.Open();

            //tao style
            AreaStyle areaStyle = new AreaStyle();
            switch (DDTypeStyle.SelectedValue)
            {
                case "Hatch":
                    GeoHatchBrush hatchBrush = new GeoHatchBrush(Constants.getStyle.getstyleHatch(DDStyleHatch.SelectedValue), GeoColor.FromHtml(txtColorHatch.Text));
                    hatchBrush.BackgroundColor = GeoColor.FromHtml(txtColorBackHatch.Text.Trim());
                    areaStyle.Advanced.FillCustomBrush = hatchBrush;
                    break;
                case "Solid": GeoSolidBrush SolidBrush = new GeoSolidBrush(GeoColor.FromHtml(txtColor.Text.Trim()));
                    areaStyle.Advanced.FillCustomBrush = SolidBrush;
                    break;
                case "Linear": break;
                case "Texture": break;
            }
            areaStyle.OutlinePen.Color = GeoColor.FromHtml(TxtColorBorder.Text.Trim());
            areaStyle.OutlinePen.DashStyle = Constants.getStyle.getDashStyle(DDTypeBorder.SelectedValue);
            areaStyle.OutlinePen.Width = float.Parse(txtBorderWidth.Text);
            //ep kieu style cho layer
            MsSql2008FeatureLayer sql2008Layer = new MsSql2008FeatureLayer(_Default.connectString, layerSQL, "ID", 4326);
            //sql2008Layer.ZoomLevelSet.CustomZoomLevels.Add(Constants.getScale.GetPolygonZoomLevel(DDZoomMin.SelectedValue,DDZoomMax.SelectedValue,areaStyle,null));//ZoomLevel01.CustomStyles.Add(areaStyle);
            Constants.getScale.getZoomLevel(DDZoomMin.SelectedValue,sql2008Layer).CustomStyles.Add(areaStyle);
            Constants.getScale.getZoomLevel(DDZoomMin.SelectedValue, sql2008Layer).ApplyUntilZoomLevel =
                Constants.getScale.getApplyZoomLevel(DDZoomMax.SelectedValue);
            sql2008Layer.FeatureSource.Projection = proj4;
            GeoSerializer serializer = new GeoSerializer();
            string serializeredLayerString = serializer.Serialize(sql2008Layer);
            Session["View"] = serializeredLayerString;
        }

        private void ApplyStyleLine(string layerSQL)
        {

            //he toa do
            Proj4Projection proj4 = new Proj4Projection();
            proj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
            proj4.ExternalProjectionParametersString = Proj4Projection.GetGoogleMapParametersString();
            proj4.Open();

            //tao style
            LineStyle lineStyle = new LineStyle();
            lineStyle.InnerPen.Color = GeoColor.FromHtml(txtColor.Text.Trim());
            lineStyle.InnerPen.Width = float.Parse(txtBorderWidth.Text.Trim());
            lineStyle.InnerPen.DashStyle = Constants.getStyle.getDashStyle(DDTypeBorder.SelectedValue);
            lineStyle.OuterPen.Color = GeoColor.FromHtml(TxtColorBorder.Text.Trim());
            lineStyle.OuterPen.Width = float.Parse(txtBorderWidth.Text.Trim());
            lineStyle.OuterPen.DashStyle = Constants.getStyle.getDashStyle(DDTypeBorder.SelectedValue);
            //ep kieu style cho layer
            MsSql2008FeatureLayer sql2008Layer = new MsSql2008FeatureLayer(_Default.connectString, layerSQL, "ID", 4326);
            Constants.getScale.getZoomLevel(DDZoomMin.SelectedValue, sql2008Layer).CustomStyles.Add(lineStyle);
            Constants.getScale.getZoomLevel(DDZoomMin.SelectedValue, sql2008Layer).ApplyUntilZoomLevel =
                Constants.getScale.getApplyZoomLevel(DDZoomMax.SelectedValue);
            sql2008Layer.FeatureSource.Projection = proj4;
            GeoSerializer serializer = new GeoSerializer();
            string serializeredLayerString = serializer.Serialize(sql2008Layer);
            Session["View"] = serializeredLayerString;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Session["Type"].ToString() == Constants.POLYGON)
                ApplyStyleAreA(DDLayer.SelectedValue);
            else if (Session["Type"].ToString() == Constants.POLYLINE)
                ApplyStyleLine(DDLayer.SelectedValue);
            //cap nhat GeoSerializer vao csdl
            data.CheckExistStyleColumn(DDLayer.SelectedValue);
            data.UpdateStyleLayerMap(DDLayer.SelectedValue, Session["View"].ToString());

        }

        protected void DDTypeStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (DDTypeStyle.SelectedValue)
            {
                case "Hatch":
                    txtColor.Enabled = false;txtColorHatch.Enabled = true;
                    txtColorBackHatch.Enabled = true;
                    DDStyleHatch.Enabled = true;
                    break;
                case "Solid":
                    txtColor.Enabled = true;
                    txtColorHatch.Enabled = false;
                    txtColorBackHatch.Enabled = false;
                    DDStyleHatch.Enabled = false;
                    break;
                default:break;
            }
        }
        private ZoomLevelSet CustomZoomlevelSets()
        {
            ZoomLevelSet myZoomlevelset = new ZoomLevelSet();

            GoogleMapsZoomLevelSet googlezoomlevelsets = new GoogleMapsZoomLevelSet();
            foreach (ZoomLevel item in googlezoomlevelsets.GetZoomLevels().Skip(13).Take(13))
            {
                myZoomlevelset.CustomZoomLevels.Add(item);
            }

            return myZoomlevelset;
        }
        protected void DDLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            MsSql2008FeatureLayer msSql2008=new MsSql2008FeatureLayer(_Default.connectString,DDLayer.SelectedValue,"ID",4326);
            msSql2008.Open();
            LblType.Text = msSql2008.GetFirstGeometryType().ToString();
            Label11.Text="  >>Kiểu "+msSql2008.GetFirstGeometryType().ToString();
            Map1.CustomOverlays.Clear();

        }

        protected void BtnView_Click(object sender, EventArgs e)
        {
            if (LblType.Text == Constants.POLYGON)
                ApplyStyleAreA(DDLayer.SelectedValue);
            else if (LblType.Text == Constants.POLYLINE)
                ApplyStyleLine(DDLayer.SelectedValue);
            if (Session["View"] != "")
            {
                string serializered = Session["View"].ToString();
                GeoSerializer serializer = new GeoSerializer();
                MsSql2008FeatureLayer deSerializedLayer = serializer.Deserialize(serializered) as MsSql2008FeatureLayer;
                Map1.CustomOverlays.Clear();
                GoogleOverlay mapgg4 = new GoogleOverlay();
                mapgg4.GoogleMapType = GoogleMapType.Normal;
                mapgg4.Name = "google";
                Map1.CustomOverlays.Add(mapgg4);
                Map1.ZoomLevelSet = CustomZoomlevelSets();
                LayerOverlay staticOverlay = new LayerOverlay("test", false, TileType.SingleTile);
                staticOverlay.Layers.Add("xml", deSerializedLayer);
                deSerializedLayer.Open();
                Map1.CustomOverlays.Add(staticOverlay);
                Map1.CurrentExtent = deSerializedLayer.GetBoundingBox();
                
            }

        }

       
    }
}